/**
 * 一些插件的封装和共用方法
 */
var QTYYComponentsTree = function() {

	var initTree = function(treeId, setting) {
		// 初始化树
		var $functionTree = $.fn.zTree.init($("#" + treeId), setting, null);
		return $functionTree;
	}

	var refreshTree = function(treeId) {
		parent.$.fn.zTree.getZTreeObj(treeId).reAsyncChildNodes(null, "refresh");
	}

	/**
	 * form表单ajax提交封装方法
	 */
	var ajaxFormSumbitTree = function(ajaxUrl, formId, treeId, type) {
		swal({
			title : "确定提交表单?",
			text : "",
			type : "warning",
			showCancelButton : true,
			confirmButtonClass : "btn-danger",
			confirmButtonText : "确定",
			cancelButtonText : "取消",
			closeOnConfirm : false,
			closeOnCancel : false
		}, function(isConfirm) {
			if (isConfirm) {
				$.ajax({
					type : "POST",
					cache : false,
					url : ajaxUrl,
					data : $("#" + formId).serialize(),
					success : function(responseJson) {
						if (responseJson.success == true) {
							var index = parent.layer.getFrameIndex(window.name);
							parent.layer.close(index);
							var treeObj = parent.$.fn.zTree.getZTreeObj(treeId);
							if (type == "add") {
								var pNode = treeObj.getNodeByParam("id", responseJson.obj.pId, null);
								treeObj.addNodes(pNode, responseJson.obj);
							}
							if (type == "update") {
								var node = treeObj.getNodeByParam("id", responseJson.obj.id, null);
								node.name = responseJson.obj.name;
								treeObj.updateNode(node);
							}
							QTYYComponents.bootstrapSweetAlert("", responseJson.msg, "success");
						} else {
							QTYYComponents.bootstrapSweetAlert("", responseJson.msg, "error");
						}
					},
					beforeSend : function(XMLHttpRequest) {
						// 请求之前方法增强处理 ,显示遮罩层
						App.blockUI({
							target : '#' + formId,
							animate : true
						});
					},
					complete : function(XMLHttpRequest, textStatus) {
						// 请求结束方法增强处理 ,隐藏遮罩层
						App.unblockUI('#' + formId);
					},
					error : function(XMLHttpRequest, textStatus, errorThrown) {
						QTYYComponents.bootstrapSweetAlert("", "处理失败，请稍候重试", "error");
					}
				});
			} else {
				swal("", "已经取消了当前操作", "error");
			}
		});
	}

	var ajaxDeleteTree = function(ajaxUrl, treeId, treeNode) {
		swal({
			title : "确定删除吗?",
			text : "",
			type : "warning",
			showCancelButton : true,
			confirmButtonClass : "btn-danger",
			confirmButtonText : "确定",
			cancelButtonText : "取消",
			closeOnConfirm : false,
			closeOnCancel : false
		}, function(isConfirm) {
			if (isConfirm) {
				var idDataArray = [];
				idDataArray.push(treeNode.id);
				$.ajax({
					type : 'POST',
					url : ajaxUrl,
					contentType : "application/json",
					data : JSON.stringify(idDataArray),
					dataType : "json",
					success : function(responseJson) {
						if (responseJson.success == true) {
							var treeObj = parent.$.fn.zTree.getZTreeObj(treeId);
							treeObj.removeNode(treeNode);
							QTYYComponents.bootstrapSweetAlert("", responseJson.msg, "success");
						} else {
							QTYYComponents.bootstrapSweetAlert("", responseJson.msg, "error");
						}
					},
					error : function(response) {
						QTYYComponents.bootstrapSweetAlert("", "处理失败，请稍候重试", "error");
					}
				});
			} else {
				swal("", "已经取消了当前操作", "error");
			}
		});
	}

	return {
		initTree : function(treeId, setting) {
			return initTree(treeId, setting);
		},
		refreshTree : function(treeId) {
			refreshTree(treeId);
		},
		ajaxFormSumbitTree : function(ajaxUrl, formId, treeId, type) {
			ajaxFormSumbitTree(ajaxUrl, formId, treeId, type);
		},
		ajaxDeleteTree : function(ajaxUrl, treeId, treeNode) {
			ajaxDeleteTree(ajaxUrl, treeId, treeNode);
		}
	}
}();